<template>
	<section>
		<!--工具条-->

		<el-row class="toolbar" style="padding-bottom: 0px;" type="flex" size='medium'>
			<el-col :span="6" >
				<el-button type="primary" @click="handleAdd" size='medium'>新增用户</el-button>
			</el-col>
			<el-col :span="18"  size='medium'>
				<el-form :inline="true" :model="selections"  style="float:right" >
						<el-form-item >
						<el-input v-model="selections.name" style="width:140px;" placeholder="输入姓名" size='medium'>
						</el-input>
					</el-form-item>
					<el-form-item >
						<el-select v-model="selections.province" style="width:140px;" placeholder="选择省份" size='medium'>
								 <el-option v-for="province in provinceList"
														:key="province.province"
														:value="province.province"
														:label="province.province">
									</el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" v-on:click="getUsers" size='medium'>查询</el-button>
					</el-form-item>
				</el-form>
			</el-col>
			
		</el-row>
		
		<!--列表-->
		<el-table  :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" border 
		:header-cell-style="{background:'#204BD6',color:'#fff'}"
		style="width: 100%;">
			<el-table-column prop="id" label="序号" width="100" header-align='center' sortable>
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="100" header-align='center' sortable>
			</el-table-column>
			<el-table-column prop="province" label="省份"  header-align='center' sortable>
			</el-table-column>
			<el-table-column prop="account" label="账号"  header-align='center' sortable>
			</el-table-column>
			<!-- <el-table-column prop="password" label="密码"  header-align='center'>
					<template slot-scope="scope">
			
				</template>
			</el-table-column> -->
			<el-table-column prop="role" label="角色"  header-align='center' sortable>
			</el-table-column>

			<el-table-column label="操作" width="100" header-align='center'>
				<template scope="scope">
					<el-button type="text" size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="text" size="small" @click="handleStates(scope.$index, scope.row)">{{scope.row.enable===true?'禁用':'启用'}}</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" >
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增用户" :visible.sync="userAddVisible" :close-on-click-modal="false" width="400" >
			<userAdd ></userAdd>
		</el-dialog>
			<!--修改界面-->
		<el-dialog title="用户修改" :visible.sync="userModifyVisible" :close-on-click-modal="false" >
			<userModify :forms='transRow'></userModify>
		</el-dialog>
	</section>
</template>

<script>
	import axios from 'axios'
	import provinceCodeList from "../../utils/provinceCode";
	import {user_info_page ,user_info_update} from '../../api/api';
	import userAdd from './userAdd';
	import userModify from './userModify';
	export default {
		components:{userAdd,userModify},
		data() {
			return {
				provinceList:provinceCodeList,
				userModifyVisible:false,
				userAddVisible: false,//编辑界面是否显示
				selections:{
					name:'',
					courses:'',//科目
					provinces:'',//选择省份
				},
				transRow:{},
				exampaper:[],
				users: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
			format:function(paper) {
				return (percentage)=>{
					return `已上传${paper.total}张`
				}
			},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
        axios.defaults.headers.common["Art-Token"] = sessionStorage.getItem('Art-Token');
				let para = {
					page: this.page,
					name: this.selections.name,
					province:this.selections.province,
					current:this.page,
					size:20
				};
				this.listLoading = true;
				user_info_page(para).then((res) => {
					this.total = res.result.total;
					this.users = res.result.records;
					this.users.sort((a,b)=>{return (a.id > b.id) ? 1 : -1})
					this.listLoading = false;
				});
			},
			//删除
			handleQuery: function (index, row) {
				this.$store.state.studio.name=row.studioname
				this.$store.state.studio.studentsCount=row.studentsCount
				this.$router.push({path: '/studioDetail'})
			},
			//显示编辑
			handleAdd: function () {
				this.userAddVisible = true;
			},
			//显示编辑
			handleEdit: function (idx,row) {
				this.transRow=row
				this.transRow['provinces']=row.province.split(',')
				console.log(this.transRow['provinces'])
				this.userModifyVisible = true;
			},
			handleStates:function (idx,row){
				this.transRow=row
				this.transRow['provinces']=row.province.split(',')
				this.transRow.enable=!this.transRow.enable
				user_info_update(this.transRow).then((res)=>{
					console.log(res)
				})

			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			axios.defaults.headers.common["Art-Token"] = sessionStorage.getItem('Art-Token');
			this.exampaper=this.$store.state.exampaper
			this.getUsers();
		}
	}

</script>

<style>
.el-button--small{
	padding: 1px 1px;
}
.el-dialog__header{
	text-align: center;
}
.el-dialog__title{
	font-size: 32px;
}
</style>